#/bin/bash
i="脚本说明:通过二进制的方式在Centos7系统进行K8S的部署"
##创建日期:2020年7月30日
##By：信仰
info="项目主页：https://gitee.com/faith01238/shells_Kubernetes
个人Gitee主页：https://gitee.com/faith01238"
##定义字体效果
#红色背景
red_bg='\e[41;33;1m'
#红色字体
red_zt='\e[1;31m'
#绿色背景
green_bg='\033[0;32;1m'
#绿色字体
green_zt='\e[1;32m'
#关闭效果
end='\033[0m'
#字体闪烁
ss='\033[05m'
#消隐
sy='\33[08m'
############################打印函数
Loading(){
	echo -e "${green_bg}${ss}${green_bg}正在进入...${end}${end}${end}\n${red_zt}${1}...${end}"
}
e_info(){
	echo -e "${red_zt}${1}:${end}${green_zt}${2}${end}"
}
cheks(){
	echo -e "${red_zt}正在检测${1}...${end}"
}
g_zt(){
	echo -e "${green_zt}${1}${end}"
}
r_zt_g_zt(){
	echo -e "${red_zt}${1}${end}${green_zt}${2}${end}${green_zt}${3}${end}"
}
r_zt(){
	echo -e "${red_zt}${1}${end}"
}
r_bg(){
	echo -e "${red_bg}${1}${end}"
}
r_zt_bg(){
	echo -e "${red_zt}${1}${end}${red_bg}${2}${end}"
}
input(){
	echo -e "${red_zt}${1}${end}"
	read $2
}
g_zt "$i\n$info\nLoading...\n信息录入"
host_ip(){
    input "请输入${1}IP地址" "$2"
}
Information_confirmed(){
    r_zt "预安装服务信息如下:"
    e_info "本机节点类型:" "$host"
    e_info "Master节点IP地址: " "$Master_ip"
    if [[ "$host" == "Master" ]];then
        e_info "预设Node1节点IP地址" "$node_ip"
        e_info "预设Node1节点主机名" "$Node_host"
        e_info "预设etcd节点IP地址" "$etcd_ip"
        e_info "预设etcd节点主机名" "$etcd_host"
    else
        e_info "本机IP地址:" "$ip"
    fi
    input "确认安装请输入[y]" "Y"
    if [[ "$Y" == "y" || "$Y" == "Y" ]];then
        g_zt "进入安装配置"
    else
        r_zt_g_zt "信息有误" "程序终止"
    fi
}
host_chek(){
    if [[ "$1" == "$2" || "$1" == "$3" || "$2" == "$3" ]];then
        r_bg "存在重复${4}信息,程序终止"
        exit 12
    fi
}
hosts(){
    echo -e "${Master_ip} ${master_name}" >> /etc/hosts
    if [[ "$host" == "etcd" ]];then
        host_chek "$etcd_ip" "$Master_ip" "1" "IP"
        host_chek "$etcd_name" "$master_name" "1" "主机名"
        echo -e "${etcd_name}\t${etcd_ip}" >> /etc/hosts
    elif [[ "$host" == "Node" ]];then
        host_chek "$node_ip" "$Master_ip" "1" "IP"
        host_chek "$Node_host" "$master_name" "1" "主机名"
        echo -e "${node_ip}\t${node_name}" >> /etc/hosts
    elif [[ "$host" == "Master" ]];then
        input "请输入预设Node1节点IP地址" "node_ip" "1" "IP"
        input_chek "Node1节点IP地址" "$node_ip" "ip" "1" "主机名"
        input "请输入预设Node1节点主机名[默认node1]" "Node_host"
        input_chek "node1" "$Node_host" "hostname"
        input "请输入预设etcd节点IP地址" "etcd_ip" 
        input_chek "etcd节点IP地址" "$etcd_ip" "ip"
        input "请输入预设etcd节点主机名[默认etcd1]" "etcd_host"
        input_chek "etcd1" "$etcd_host" "hostname"
        host_chek "$node_ip" "$Master_ip" "$etcd_ip"
        host_chek "$Node_host" "$etcd_host" "$master_name"
    fi

}
input_chek(){
    if [[ -z "$2" ]];then
        # r_zt_bg "$1" "$2"
        if [[ "$3" == "ip" ]];then
            ip=`hostname -I`
            if [[ -z "$2" ]];then
                r_zt_g_zt "未检测到输入信息,请选择一个IP:\n" "$ip"
            else
                $3
            fi
        fi
        if [[ "$3" == "hostname" ]];then
                if [[ "$4" == "set" ]];then
                    hostnamectl set-hostname $1
                    g_zt "已设置默认主机名[${1}]"
                fi
                ##二参为IP地址，二参为主机名
                echo -e "${2}\t${1}" >> /etc/hosts
        fi
    fi
}
sysct(){
    cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
    sysctl --system 
}
host_type(){
    input "请选择本机主机类型\n[1]Master\n[2]Node\n[3]etcd\tDefault[2]" "host"
    if [[ "$host" == "1" ]];then
        host="Master"
        ##设置本机IP地址
        host_ip "本机" "Master_ip" "ip"
        input "请设置本机主机名,默认[Master]" "master_name"
        input_chek "Master" "$master_name" "hostname" "set"
    elif [[ "$host" == "3" ]];then
        host="etcd"
        #设置Master主机IP
        host_ip "Master" "Master_ip"
        #设置Master主机名
        input "请输入Master主机名" "Master_name"
        #设置本机IP地址
        host_ip "本机" "etcd_ip"
        input_chek "本机[etcd]IP地址" "$etcd_ip" "ip"
        input "请设置本机主机名,默认[etcd]" "etcd_name"
        input_chek "etcd" "$etcd_name" "hostname" "set"
    else
        host="Node"
        #设置Master主机IP
        host_ip "Master" "Master_ip"
        #设置Master主机名
        input "请输入Master主机名" "Master_name"
        #设置本机IP地址
        host_ip "$host" "node_ip"
        input_chek "本机[Node]IP地址" "$node_ip" "ip"
        input "请设置本机主机名,默认[Node1]" "node_name"
        input_chek "Node1" "$node_name" "hostname" "set"
    fi
    input_chek "Master节点IP地址" "$Master_ip" "host_type"
}
#####################进入安装流程
e_info "正在设置主机信息"
host_type
hosts
Information_confirmed
e_info "正在设置路由转发"
sysct
e_info "正在设置主机映射"
hosts